<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书管理 </title>
    <link rel="stylesheet" href="/librarymanagementsystem/css/main.css">
    <script src="/librarymanagementsystem/js/jquery-3.2.1.js"></script>
    <script type="text/javascript">
        $(function() {
            var params = new URLSearchParams(window.location.search);
            var pageNo = params.get("pageNo");
            var likeValue = params.get("likeValue");
            
            // 显示操作结果消息
            var message = params.get("message");
            var error = params.get("error");
            
            if (message === "delete_success") {
                alert("图书删除成功！");
            } else if (message === "save_success") {
                alert("图书保存成功！");
            } else if (error === "delete_failed") {
                alert("图书删除失败！可能该图书已被借出或不存在。");
            } else if (error === "save_failed") {
                alert("图书保存失败！");
            } else if (error === "invalid_id") {
                alert("无效的图书ID！");
            } else if (error === "missing_id") {
                alert("缺少图书ID参数！");
            }
            
            // 调用分页查询方法
            queryByPage(pageNo || 1, likeValue || "");
            
            // 点击查询按钮
            $("#search-btn").click(function() {
                var searchValue = $("#search-input").val();
                queryByPage(1, searchValue);
            });
            
            // 回车搜索
            $("#search-input").keypress(function(e) {
                if (e.which == 13) {
                    var searchValue = $(this).val();
                    queryByPage(1, searchValue);
                }
            });
        });
        
        // 分页查询
        function queryByPage(pageNo, likeValue) {
            $("#book-table-body").empty();
            $("#book-table-body").html("<tr><td colspan='10'>加载中...</td></tr>");
            
            var url = "/librarymanagementsystem/list.book";
            var params = {
                "pageNo": pageNo,
                "likeValue": likeValue
            };
            
            $.get(url, params, function(data) {
                if (!data) {
                    alert("获取数据失败，请检查服务器状态！");
                    return;
                }
                
                // 解析返回数据
                var recordList = data.recordList;
                if (!recordList) {
                    $("#book-table-body").html("<tr><td colspan='10'>暂无图书记录</td></tr>");
                    return;
                }
                
                // 清空表格内容
                $("#book-table-body").empty();
                
                // 遍历记录并添加到表格
                for (var i = 0; i < recordList.length; i++) {
                    var book = recordList[i];
                    var id = book.id;
                    var isbn = book.isbn || "";
                    var title = book.title || "";
                    var author = book.author || "";
                    var publisher = book.publisher || "";
                    var publishDate = formatDate(book.publish_date);
                    var price = book.price || 0;
                    var category = book.category || "";
                    var status = book.status || "";
                    
                    var statusClass = "";
                    if (status === "借出") {
                        statusClass = "borrowed";
                    }
                    
                    var row = "<tr>" +
                        "<td>" + id + "</td>" +
                        "<td>" + isbn + "</td>" +
                        "<td>" + title + "</td>" +
                        "<td>" + author + "</td>" +
                        "<td>" + publisher + "</td>" +
                        "<td>" + publishDate + "</td>" +
                        "<td>" + price + "</td>" +
                        "<td>" + category + "</td>" +
                        "<td class='" + statusClass + "'>" + status + "</td>" +
                        "<td>" + 
                            "<a href='toEdit.book?id=" + id + "' class='btn-small'>编辑</a> " +
                            "<a href='deleteById.book?id=" + id + "' class='btn-small' onclick='return confirmDelete(\"" + title + "\")'>删除</a> " +
                            (status === "在库" ? "<button class='btn-small' onclick='borrowBook(" + id + ")'>借阅</button>" : "") +
                        "</td>" +
                        "</tr>";
                    
                    $("#book-table-body").append(row);
                }
                
                // 更新分页信息
                $("#likeValueStr").val(data.likeValue);
                $("#recordCount").text(data.recordCount);
                $("#totalPages").text(data.totalPages);
                $("#pageNo").text(data.pageNo);
                
                // 更新分页链接
                $("#homePage").attr("href", "book_page.html?pageNo=1&likeValue=" + data.likeValue);
                
                if (parseInt(data.pageNo) > 1) {
                    $("#prevPage").attr("href", "book_page.html?pageNo=" + (parseInt(data.pageNo) - 1) + "&likeValue=" + data.likeValue);
                    $("#prevPage").removeClass("disabled");
                } else {
                    $("#prevPage").attr("href", "javascript:void(0)");
                    $("#prevPage").addClass("disabled");
                }
                
                if (parseInt(data.pageNo) < parseInt(data.totalPages)) {
                    $("#nextPage").attr("href", "book_page.html?pageNo=" + (parseInt(data.pageNo) + 1) + "&likeValue=" + data.likeValue);
                    $("#nextPage").removeClass("disabled");
                } else {
                    $("#nextPage").attr("href", "javascript:void(0)");
                    $("#nextPage").addClass("disabled");
                }
                
                $("#lastPage").attr("href", "book_page.html?pageNo=" + data.totalPages + "&likeValue=" + data.likeValue);
            });
        }
        
        // 格式化日期
        function formatDate(dateStr) {
            if (!dateStr) return "";
            
            var date = new Date(dateStr);
            if (isNaN(date.getTime())) {
                return dateStr;
            }
            
            var year = date.getFullYear();
            var month = (date.getMonth() + 1).toString().padStart(2, '0');
            var day = date.getDate().toString().padStart(2, '0');
            
            return year + "-" + month + "-" + day;
        }
        
        // 删除图书
        function deleteBook(id) {
            if (confirm('确定要删除这本图书吗？')) {
                window.location.href = 'deleteById.book?id=' + id;
            }
        }
        
        // 确认删除
        function confirmDelete(title) {
            return confirm('确定要删除《' + title + '》这本图书吗？');
        }
        
        // 编辑图书
        function editBook(id) {
            window.location.href = 'book_add.html?id=' + id;
        }
        
        // 借阅图书
        function borrowBook(bookId) {
            window.location.href = "/librarymanagementsystem/borrow_add.html?bookId=" + bookId;
        }
    </script>
</head>
<body>
    <div class="container">
        <header>
            <h1>图书管理系统</h1>
            <nav>
                <ul>
                    <li><a href="/librarymanagementsystem/index.html">首页</a></li>
                    <li><a href="/librarymanagementsystem/book_page.html" class="active">图书管理</a></li>
                    <li><a href="/librarymanagementsystem/borrow_page.html">借阅管理</a></li>
                </ul>
            </nav>
        </header>
        
        <main>
            <h2>图书列表</h2>
            
            <div class="action-bar">
                <a href="/librarymanagementsystem/book_add.html" class="btn">添加图书</a>
                
                <div class="search-bar">
                    <input type="text" id="search-input" placeholder="输入ID、ISBN、书名、作者、分类或状态...">
                    <button id="search-btn">查询</button>
                </div>
            </div>
            
            <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>ISBN</th>
                        <th>书名</th>
                        <th>作者</th>
                        <th>出版社</th>
                        <th>出版日期</th>
                        <th>价格</th>
                        <th>分类</th>
                        <th>状态</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody id="book-table-body">
                    <tr>
                        <td colspan="10">加载中...</td>
                    </tr>
                </tbody>
            </table>
            
            <div class="pagination">
                <span>总记录数: <span id="recordCount">0</span></span>
                <span>总页数: <span id="totalPages">0</span></span>
                <a href="#" id="homePage">首页</a>
                <a href="#" id="prevPage">上一页</a>
                <a href="#" id="nextPage">下一页</a>
                <a href="#" id="lastPage">末页</a>
                <span>当前第 <span id="pageNo">1</span> 页</span>
            </div>
        </main>
        
        <footer>
            <p>&copy; 2025 图书管理系统</p>
        </footer>
    </div>
</body>
</html> 